﻿using Orchard.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Vitus.MediaProcessing.Models;

namespace Vitus.MediaProcessing.Services
{
    public class MediaProfileAttachmentService : IMediaProfileAttachmentService
    {
        private readonly IRepository<MediaProfileAttachmentRecord> attachmentRepository;

        public MediaProfileAttachmentService(IRepository<MediaProfileAttachmentRecord> attachmentRepository)
        {
            this.attachmentRepository = attachmentRepository;
        }

        public IEnumerable<MediaProfileAttachmentRecord> GetMediaProfileAttachments(int contentId)
        {
            return attachmentRepository.Fetch(record => record.MediaProfileAttachmentsPartRecord.Id == contentId);
        }

        public void AddMediaProfileAttachment(int contentId, string mediaPath, string profileName)
        {
            attachmentRepository.Create(new MediaProfileAttachmentRecord
                {
                    MediaProfileAttachmentsPartRecord = new MediaProfileAttachmentsPartRecord { Id = contentId },
                    MediaPath = mediaPath,
                    ProfileName = profileName
                });
        }

        public void DeleteMediaProfileAttachment(int id)
        {
            var record = attachmentRepository.Get(id);

            attachmentRepository.Delete(record);
        }

        public void DeleteMediaProfileAttachments(int contentId)
        {
            var records = GetMediaProfileAttachments(contentId);

            foreach (var record in records)
            {
                attachmentRepository.Delete(record);
            }
        }
    }
}